package com.koron.rating.units;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.koron.bean.base.Response;
import com.koron.bean.system.org.OrgBean;
import com.koron.bean.system.post.query.PostUserQuery;
import com.koron.bean.system.post.vo.PostUserVo;
import com.koron.bean.system.role.AppOrgJurisdictionBean;
import com.koron.bean.system.role.UserOwnJurisdictionBean;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.business.system.feign.SystemFeignService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.StringUtils;
import com.koron.rating.classify.bean.ImpEqBo;
import com.koron.rating.enums.RatingCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: rocky
 * @Date: 2022/1/27 11:51
 */
@Component
public class RatingUnits {

    @Autowired
    private SystemFeignService systemfeignservice;

    public static RatingUnits ratingunits;

    @PostConstruct  //关键四：通过@PostConstruct注解实现注入
    public void init() {
        ratingunits = this;
    }

    /**
     * 调公共接口查审批人员
     * 查出的人员 都有岗位的
     *
     * @param postUserQuery
     * @return
     */
    public static List<PostUserVo> getWorkflowUsers(PostUserQuery postUserQuery) {
        Response<List<PostUserVo>> listResponse = ratingunits.systemfeignservice.queryPostList(postUserQuery);//查出符合条件的审批人
        Assert.isFalse(listResponse.getCode() != 200, listResponse.getDescription());
        if (listResponse.getCode() == 200) {
            List<PostUserVo> ulist = listResponse.getData();
            if (StringUtils.isEmpty(ulist)) {
                return new ArrayList();
            }
            return ulist;
        }
        return null;
    }

    /**
     * 调公共接口
     * 查出 单位的上级部门 或者自身部门
     *
     * @param orgId 单位id、部门id
     * @return
     */
    public static String queryDeptByOrgId(String orgId) {
        Response<OrgBean> response = ratingunits.systemfeignservice.queryDeptByOrgId(orgId);//查出符合条件的审批人
        Assert.isFalse(response.getCode() != 200, response.getDescription());
        if (response.getCode() == 200) {
            OrgBean orgbean = response.getData();
            if (ObjectUtil.isNotNull(orgbean)) {
                return orgbean.getId();
            }
        }
        return null;
    }


    //获取岗位信息
    public static List<String> getPostId(SubmitterParam submitterParam) {
        return submitterParam.getUsers().stream().filter(u -> u.getType() == 2).map(WorkflowUser::getCode).collect(Collectors.toList());
    }

    //获取岗位信息
    public static List<String> getDeptId(SubmitterParam submitterParam) {
        return submitterParam.getUsers().stream().filter(u -> u.getType() == 1).map(WorkflowUser::getCode).collect(Collectors.toList());
    }


    /**
     * 获取评级单编号、
     */
    public static String getRatingCode(RatingCodeEnum codeEnum) {
        EamUser eamUser = ThreadLocalContext.get();
        String orgPrefix = eamUser.getPrefix();
        if (orgPrefix == null) {
            orgPrefix = "";
        }
        return StrUtil.replace(CodeTools.getCurrAppCodeNo(codeEnum.getValue() + orgPrefix, "yyyyMMdd", 3), orgPrefix, "");
    }

    /**
     * 获取单元变更单编号
     */
    public static String getRatingChangeCode(RatingCodeEnum codeEnum) {
        EamUser eamUser = ThreadLocalContext.get();
        String orgPrefix = eamUser.getPrefix();
        if (orgPrefix == null) {
            orgPrefix = "";
        }
        return CodeTools.getCurrAppCodeNo(codeEnum.getValue(), "yyyyMMdd", 3);
    }

    private EamUser getEamUser() {
        return ThreadLocalContext.get();
    }

    /**
     * 获取权限
     * @return
     */
    public static ArrayList<String> getPower(EamUser user) {
        ArrayList<String> units = CollUtil.newArrayList(user.getDeptId());
        List<AppOrgJurisdictionBean> appOrgJurisdictionBeans = Optional.ofNullable(user)
                .map(EamUser::getUserOwnJurisdictionBean)
                .map(UserOwnJurisdictionBean::getAppOrgJurisdictionBeanList).orElse(new ArrayList<>());
        Optional<AppOrgJurisdictionBean> firstopt = appOrgJurisdictionBeans.stream().filter(j -> user.getAppId().equals(j.getAppId())).findFirst();
        if (firstopt.isPresent()) {
            units.addAll(firstopt.get().getOrgStrList());
        }
        return units;
    }


    // 正序递归 实体
    public static void recursionEq(List<Tree> jsonToList, List<ImpEqBo> insertlist) {
        for (Tree vo: jsonToList) {
            ImpEqBo convert = Convert.convert(ImpEqBo.class, vo);
            insertlist.add(convert);
            if (CollUtil.isNotEmpty(vo.getChildren())) {
                recursionEq(vo.getChildren(), insertlist);
            }
        }
    }

}
